﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCCN.DAL;

namespace SCCN.BLL
{
    public class SysUserBLL
    {
        private static SysUserBLL instance = null;

        /// <summary>
        /// 单例构造
        /// </summary>
        /// <returns></returns>
        public static SysUserBLL getInstance()
        {
            if (instance == null)
            {
                instance = new SysUserBLL();
            }
            return instance;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userpwd"></param>
        /// <returns></returns>
        public SC_SysUser UserLogin(string username, string userpwd)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_SysUser obj = entities.SC_SysUser.FirstOrDefault(a => a.UserName == username && a.UserPWD == userpwd);
                if (obj != null)
                {
                    return obj;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 根据用户名查询
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public SC_SysUser GetUserByUserId(int userId)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_SysUser.FirstOrDefault(a => a.Id == userId);
            }
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Update(SC_SysUser obj)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_SysUser objSC_SysUser = entities.SC_SysUser.FirstOrDefault(v => v.Id == obj.Id);
                entities.Detach(objSC_SysUser);
                objSC_SysUser.UserPWD = obj.UserPWD;
                objSC_SysUser.IsDelete = obj.IsDelete;
                entities.Attach(objSC_SysUser);
                var newCategory = entities.ObjectStateManager.GetObjectStateEntry(objSC_SysUser);
                newCategory.SetModified();
                newCategory.SetModifiedProperty("UserPWD");
                newCategory.SetModifiedProperty("IsDelete");
                return entities.SaveChanges() > 0 ? true : false;
            }
        }
    }
}
